package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.entity.Parking;
import com.community.entity.ParkingIn;
import com.community.entity.ParkingInmanage;
import com.community.entity.ParkingPlace;
import com.community.service.ParkingInService;
import com.community.service.ParkingInmanageService;
import com.community.service.ParkingPlaceService;
import com.community.service.ParkingService;
import com.community.util.Voice;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * (ParkingIn)表控制层
 *
 * @author makejava
 * @since 2021-11-09 22:18:48
 */
@Controller
@RequestMapping("parkingIn")
public class ParkingInController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ParkingInService parkingInService;
    @Resource
    private ParkingPlaceService parkingPlaceService;
    @Resource
    ParkingService parkingService;
    @Resource
    private ParkingInmanageService parkingInmanageService;
    @Resource
    Voice voice;

    @RequestMapping("parking_in")
    public String parking_in() {
        return "parking_in";
    }

    /**
     * 分页查询所有数据
     *
     * @param page      分页对象
     * @param parkingIn 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    @ResponseBody
    public R selectAll(Page<ParkingIn> page, ParkingIn parkingIn) {
        return success(this.parkingInService.page(page, new QueryWrapper<>(parkingIn).orderByDesc("in_date")));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ResponseBody
    public R selectOne(@PathVariable Serializable id) {
        return success(this.parkingInService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param parkingIn 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @ResponseBody
    public R insert(ParkingIn parkingIn) {
        parkingIn.setInDate(new Date());
        return success(this.parkingInService.save(parkingIn));
    }

    /**
     * 修改数据
     *
     * @param parkingIn 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ResponseBody
    public R update(@RequestBody ParkingIn parkingIn) {
        return success(this.parkingInService.updateById(parkingIn));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ResponseBody
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.parkingInService.removeByIds(idList));
    }

    /**
     * 查询车辆管理是否存在该车辆
     *
     * @param licensePlate 车牌号
     * @return
     */
    @GetMapping("in")
    @ResponseBody
    public Object selectcount(String licensePlate) {
        if (licensePlate == null || licensePlate == "") return failed("没有识别到车牌号！");
        if (parkingService.count(new QueryWrapper<Parking>().eq("car_state", 1).eq("personnel_state", 1)) == 0)
            return failed("剩余停车位不足！");
//        voice.voice(licensePlate + "欢迎您进入本小区！");
        HashMap hashMap = new HashMap();
        ParkingPlace license_plate = parkingPlaceService.getOne(new QueryWrapper<ParkingPlace>().eq("license_plate", licensePlate));
        if (license_plate != null) {
            hashMap.put("licenseplate_in", licensePlate);
            hashMap.put("carType", "固定车");
            hashMap.put("parking", license_plate.getParking());
        } else {
            hashMap.put("licenseplate_in", licensePlate);
            hashMap.put("carType", "临时车");
            hashMap.put("parking", parkingService.randomParking());
        }
        ParkingIn parkingIn = new ParkingIn();
        parkingIn.setCarNumber((String) hashMap.get("licenseplate_in"));
        parkingIn.setCarType((String) hashMap.get("carType"));
        parkingIn.setParking((String) hashMap.get("parking"));
        parkingIn.setInDate(new Date());
        parkingInService.save(parkingIn);    //入口车辆+1

        ParkingInmanage parkingInmanage = new ParkingInmanage();
        parkingInmanage.setCarNumber((String) hashMap.get("licenseplate_in"));
        parkingInmanage.setCarType((String) hashMap.get("carType"));
        parkingInmanage.setParking((String) hashMap.get("parking"));
        parkingInmanage.setInData(new Date());
        parkingInmanage.setStatus("在场");
        parkingInmanageService.save(parkingInmanage);   //在场车辆+1

        QueryWrapper<Parking> wrapper = new QueryWrapper<>();
        wrapper.eq("stall", hashMap.get("parking"));
        Parking parking = new Parking();
        parking.setCarState(2);
        parkingService.update(parking, wrapper);
        return hashMap;
    }

    @GetMapping("randomParking")
    @ResponseBody
    public Object selectcount2() {
        return parkingService.randomParking();
    }
}

